<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        // // 1.产生一个0~100之间的随机数；
        // var randomNum = Math.ceil(Math.random() * 100);
        // console.log(randomNum);

        // // 2.prompt():显示用户输入的对话框；
        // // 
        // var guess = prompt("请输入0~100之间的数字猜猜");
        // console.log(guess);


        // // 3.比较；
        // if (randomNum > guess) {
        //     alert("猜小啦")
        // } else if (randomNum < guess) {
        //     alert('猜大啦')
        // } else {
        //     alert('猜对啦')
        // }


        function guessNumber() {
            var random_num = Math.ceil(Math.random() * 100);

            var count = 5;
            var finish = false;
            while (!finish) {

                if (count <= 0) {
                    alert('次数已经用完');
                    break;
                }

                count--;
                var guess_num = prompt("请输入你猜的数字(0~100)");

                if (random_num > guess_num) {
                    alert('猜小啦，还有' + count + '次机会')
                } else if (random_num < guess_num) {
                    alert('猜da啦，还有' + count + '次机会')
                } else {
                    alert('猜对啦')
                    finish = ture;
                }
            }
        }
        var r2 = guessNumber()
        console.log(r2)
    </script>
</body>

</html>